//
//  GGCityViewController.m
//  tour
//
//  Created by 123 on 16/5/17.
//  Copyright © 2016年 Jushi. All rights reserved.
//

#import "GGCityViewController.h"
#import "CityGroupModel.h"
#import "FMDBCityDataTool.h"

@interface GGCityViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UISearchDisplayDelegate>

@property (nonatomic,weak) UITableView *sourceTab;
@property (nonatomic,strong) NSDictionary *cityDict;
@property (nonatomic,strong) NSMutableArray *keys;//section的title
//搜索结果
@property (nonatomic,strong) NSMutableArray *searchCities;

@property (nonatomic,weak) UISearchBar *searchBar;
@property (nonatomic,strong) UISearchDisplayController *searchDC;
@property (nonatomic,weak) UIButton *locationBtn;//定位城市

@end

@implementation GGCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [super createNaviNeedsBack:YES];
    self.titleLab.text = @"城市";
    if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self getDataTable];
}
- (void)getDataTable
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ScreenW, 88)];
    [self.view addSubview:headerView];
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 44.0f)];
    searchBar.placeholder = @"搜索";
    searchBar.delegate = self;
    searchBar.barTintColor = [UIColor whiteColor];
    searchBar.barStyle = UISearchBarStyleProminent;
    self.searchBar = searchBar;
    [headerView addSubview:self.searchBar];
    
    UISearchDisplayController *aSearchDC = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
    aSearchDC.delegate = self;
    aSearchDC.searchResultsDataSource = self;
    aSearchDC.searchResultsDelegate = self;
    self.searchDC = aSearchDC;
    
    UILabel *locationLab = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(self.searchBar.frame)+(88.0-44.0-16)/2, 80, 16)];
    locationLab.text = @"定位位置";
    locationLab.textColor = [UIColor grayColor];
    locationLab.font = Font(14);
    locationLab.textAlignment = NSTextAlignmentCenter;
    [locationLab sizeToFit];
    [headerView addSubview:locationLab];
    
    UIImage *selectedImage = GetImage(@"btn_bg_login_normal");
    UIImage *normalImage = GetImage(@"btn_bg_login_normal");
    UIButton *locationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [locationBtn setFrame:CGRectMake(CGRectGetMaxX(locationLab.frame)+10, CGRectGetMaxY(self.searchBar.frame)+7, 80, 30)];
    [locationBtn setTitle:self.locaCity forState:UIControlStateNormal];
    locationBtn.titleLabel.font = Font(14.0f);
    [locationBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [locationBtn setBackgroundImage:selectedImage forState:UIControlStateHighlighted];
    [locationBtn setBackgroundImage:normalImage forState:UIControlStateNormal];
    [locationBtn addTarget:self action:@selector(location:) forControlEvents:UIControlEventTouchUpInside];
    locationBtn.tag = 0;
    self.locationBtn = locationBtn;
    [headerView addSubview:locationBtn];
    if (self.locaCity) {
        [locationBtn setTitle:self.locaCity forState:UIControlStateNormal];
    }else{
        [locationBtn setTitle:@"未定位" forState:UIControlStateNormal];
    }
    
    UITableView *sourceTab = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(headerView.frame), ScreenW, ScreenH-64-88) style:UITableViewStylePlain];
    sourceTab.delegate = self;
    sourceTab.dataSource = self;
    [sourceTab registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    sourceTab.sectionIndexColor = WineColor;
    [self.view addSubview:sourceTab];
    self.sourceTab = sourceTab;
}
- (void)location:(UIButton *)btn
{
    if (self.cityBlock) {
        self.cityBlock(nil,btn.titleLabel.text);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - tableViewDatasource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 30)];
    bgView.backgroundColor = RGBColorOne(220);
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(13, 0, 250, 30)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:20];
    [bgView addSubview:titleLabel];
    CityGroupModel *model = (tableView == self.sourceTab)?self.cities[section]:self.searchCities[section];
    titleLabel.text = model.firstLeter;
    return bgView;
}
//右边的索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.keys;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.sourceTab) {
        return self.cities.count;
    }
    [self.searchCities removeAllObjects];
    FMDBCityDataTool *tool = [FMDBCityDataTool shareTool];
    [self.searchCities addObjectsFromArray:[tool selectCityWithText:self.searchDC.searchBar.text]];
    return self.searchCities.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    CityGroupModel *groupModel = (tableView == self.sourceTab)?self.cities[section]:self.searchCities[section];
    return groupModel.cities.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    if ((tableView != self.sourceTab)&&!cell){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([UITableViewCell class])];
    }
    CityGroupModel *groupModel = (tableView == self.sourceTab)?self.cities[indexPath.section]:self.searchCities[indexPath.section];
    CityModel *model = groupModel.cities[indexPath.row];
    cell.textLabel.text = model.name;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CityGroupModel *groupModel = (tableView == self.sourceTab)?self.cities[indexPath.section]:self.searchCities[indexPath.section];
    CityModel *model = groupModel.cities[indexPath.row];
    if (self.cityBlock) {
        self.cityBlock(model,model.name);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSMutableArray *)keys
{
    if (!_keys) {
        _keys = [NSMutableArray array];
        for (CityGroupModel *model in self.cities) {
            [_keys addObject:model.firstLeter];
        }
    }
    return _keys;
}

- (NSMutableArray *)searchCities
{
    if (!_searchCities) {
        _searchCities = [NSMutableArray array];
    }
    return _searchCities;
}

@end
